package com.warren.core.io;

import cn.hutool.core.lang.Assert;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author warren
 * @create 2021-08-19 13:39
 * @Description //资源调用选择
 */
public class DefaultResourceLoader implements ResourceLoader{

    @Override
    public Resource getResource(String location) {
        Assert.notNull(location,"Location must not be null");
        //class
        if (location.startsWith(CLASSPATH_URL_PREFIX)){
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        }else {
            //url
            try{
                URL url = new URL(location);
                return new UrlResource(url);
            }catch (MalformedURLException e){
                //file
                return new FileSystemResource(location);
            }
        }
    }
}
